package com.villaday.idf.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;

import androidx.appcompat.widget.SwitchCompat;
import androidx.core.content.FileProvider;
import androidx.appcompat.widget.Toolbar;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXMiniProgramObject;
import com.tencent.mm.opensdk.modelmsg.WXMusicObject;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.modelmsg.WXVideoObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.villaday.idf.AppManager;
import com.villaday.idf.R;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.base.BaseDialog;
import com.villaday.idf.custom.ConfirmDialog;
import com.villaday.idf.custom.DownloadDialog;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.custom.NewVersionDialog;
import com.villaday.idf.custom.ShareDialog;
import com.villaday.idf.model.ConfigEntity;
import com.villaday.idf.model.UpgradeEntity;
import com.villaday.idf.util.BitMapUtil;
import com.villaday.idf.util.CacheUtil;
import com.villaday.idf.util.ConfigConstants;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.PreferencesUtil;
import com.villaday.idf.util.SharedPreferencesUtil;
import com.villaday.idf.util.ToastUtil;
import com.villaday.idf.util.ToolBarUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/*
 * 设置
 * */
public class SettingActivity extends BaseActivity implements OkHttpUtil.OnDataListener, View.OnClickListener, OkHttpUtil.OnProgressListener, BaseDialog.OnItemClickListener {


    @BindView(R.id.toolbar_text)
    Toolbar toolbarText;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.tv_cache)
    TextView tvCache;

    @BindView(R.id.swc)
    SwitchCompat swc;
    private DecimalFormat decimal = new DecimalFormat("0.0");
    private LoadingDialog loadingDialog;
    private Gson gson = new GsonBuilder().create();
    private boolean isForce = false;//是否强制升级
    private String versionName;
    private String newAppLink;
    private DownloadDialog downloadDialog;
    private String filePath;
    private Handler handler = new Handler();
    private ConfigEntity.BodyEntity shareBody;


    @Override
    protected int getViewResId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void init() {
        ToolBarUtil.initToolBar(toolbarText, "设置", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        updateCacheSize();
        tvVersion.setText("V" + PhoneUtil.getAppVersion(this));
        swc.setChecked(SharedPreferencesUtil.getAgreementTuiJian(this));


    }

    @OnClick({R.id.tv_share, R.id.tv_evaluation, R.id.tv_advice, R.id.ll_version, R.id.tv_about, R.id.ll_cache, R.id.tv_logout, R.id.swc, R.id.tv_zhuxiap})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.swc:
                SharedPreferencesUtil.saveAgreementTuiJian(this, !SharedPreferencesUtil.getAgreementTuiJian(this));
                swc.setChecked(SharedPreferencesUtil.getAgreementTuiJian(this));
                break;
            case R.id.tv_share://推荐给好友
                loadingDialog = LoadingDialog.newInstance("加载中");
                loadingDialog.show(getFragmentManager(), "loading");
                OkHttpUtil.getJSON(this, Constant.URL.config, new OkHttpUtil.OnDataListener() {
                    @Override
                    public void onResponse(String url, String json) {
                        LogeUtil.e("config", json);
                        dismissLoading();
                        ConfigEntity configEntity = gson.fromJson(json, ConfigEntity.class);
                        if ("200".equals(configEntity.getCode())) {
                            shareBody = configEntity.getBody();
                            if ("mini".equals(shareBody.getGlobal().getShare().getType())) {
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        saveImageToSdCard(SettingActivity.this, shareBody.getGlobal().getShare().getImageUrl());
                                        shareXCX();
                                    }
                                }).start();
                            } else {
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        saveImageToSdCard(SettingActivity.this, shareBody.getGlobal().getShare().getImageUrl());
                                        ShareDialog shareDialog = ShareDialog.newInstance();
                                        shareDialog.show(getFragmentManager());
                                        shareDialog.setOnItemClickListener(SettingActivity.this);
                                        //shareXCX();
                                    }
                                }).start();
                            }

                        } else {
                            ToastUtil.initToast(SettingActivity.this, configEntity.getMsg());
                        }
                    }

                    @Override
                    public void onFailure(String url, String error) {

                    }
                });


                break;
            case R.id.tv_evaluation://去评价APP
                break;
            case R.id.tv_advice://建议与反馈
            {
                Intent intent = new Intent(this, AdviceActivity.class);
                startActivity(intent);
            }

            break;
            case R.id.ll_version://当前版本
                tvVersion.setText("V" + PhoneUtil.getAppVersion(this));
                loadingDialog = LoadingDialog.newInstance("检查更新中...");
                loadingDialog.show(getFragmentManager());
                upgrade();
                break;
            case R.id.tv_about://关于爱订房
                startActivity(new Intent(this, AboutVilladayActivity.class));
                break;
            case R.id.ll_cache://清除缓存
            {
                float cache = (CacheUtil.cacheSize() + ConfigConstants.cacheSize()) / 1024.0F;

                if (cache == 0) {
                    ToastUtil.initToast(SettingActivity.this, "很干净了，没有需要清理的缓存！");
                    return;
                }
                ConfirmDialog confirmDialog = ConfirmDialog.newInstance("确定清除缓存吗?", "取消", "确认");
                confirmDialog.setOnItemClickListener(new BaseDialog.OnItemClickListener() {
                    @Override
                    public void onItemClick(View v) {
                        if (v.getId() == R.id.btn_confirmDialog) {
                            CacheUtil.clearCache();
                            ConfigConstants.clearCache();
                            tvCache.setText("0K");

                            ToastUtil.initToast(SettingActivity.this, "已经清理缓存");

                        }
                    }
                });
                confirmDialog.show(getFragmentManager());
            }
            break;
            case R.id.tv_logout://退出登录
            {
                ConfirmDialog confirmDialog = ConfirmDialog.newInstance("确认退出登录?", "取消", "确认");
                confirmDialog.setOnItemClickListener(new BaseDialog.OnItemClickListener() {
                    @Override
                    public void onItemClick(View v) {
                        if (v.getId() == R.id.btn_confirmDialog) {
                            clearData();
                        }
                    }
                });
                confirmDialog.show(getFragmentManager());
            }
            break;
            case R.id.tv_zhuxiap: {//注销
                ConfirmDialog confirmDialog = ConfirmDialog.newInstance("确认注销并退出应用?", "取消", "确认");
                confirmDialog.setOnItemClickListener(new BaseDialog.OnItemClickListener() {
                    @Override
                    public void onItemClick(View v) {
                        if (v.getId() == R.id.btn_confirmDialog) {
                            SharedPreferencesUtil.saveCheckAgreement(SettingActivity.this,false);
                            PreferencesUtil.clearData(SettingActivity.this);
                            AppManager.getAppManager().AppExit(SettingActivity.this);
                            //clearData();
                        }
                    }
                });
                confirmDialog.show(getFragmentManager());
            }
            break;
        }

    }

    private void shareXText(int i) {//0 聊天1 朋友圈 其他收藏

        WXTextObject miniProgramObj = new WXTextObject(shareBody.getGlobal().getShare().getTitle());
/*
        miniProgramObj.miniprogramType = WXMiniProgramObject.MINIPTOGRAM_TYPE_RELEASE;// 正式版:0，测试版:1，体验版:2
*/
        WXMediaMessage msg = new WXMediaMessage(miniProgramObj);
        msg.description = shareBody.getGlobal().getShare().getTitle();
        //Bitmap bitmap = BitMapUtil.getBitmap(fileName);


        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "text";
        req.message = msg;
        req.scene = i == 0 ? SendMessageToWX.Req.WXSceneSession : i == 1 ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneFavorite;
        IWXAPI api = WXAPIFactory.createWXAPI(this, Constant.ID.WeChat_APP_ID, false);

        api.sendReq(req);
    }

    private void shareXPhoto(int i) {//0 聊天1 朋友圈 其他收藏

        String imageFileName = IMAGE_NAME + ".jpg";
        File externalCacheDir = this.getExternalCacheDir();
        String fileName = externalCacheDir.getPath() + "/" + imageFileName;

        Bitmap bitmap = BitMapUtil.getBitmap(fileName);

        WXImageObject miniProgramObj = new WXImageObject(bitmap);
/*
        miniProgramObj.miniprogramType = WXMiniProgramObject.MINIPTOGRAM_TYPE_RELEASE;// 正式版:0，测试版:1，体验版:2
*/
        WXMediaMessage msg = new WXMediaMessage(miniProgramObj);
        msg.title = shareBody.getGlobal().getShare().getTitle();                 // 小程序消息title
        msg.description = shareBody.getGlobal().getShare().getDesc();
        //Bitmap bitmap = BitMapUtil.getBitmap(fileName);

        msg.thumbData = BitMapUtil.compressBitmap(bitmap, 30 * 1024);// 小程序消息desc

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "img";
        req.message = msg;
        req.scene = i == 0 ? SendMessageToWX.Req.WXSceneSession : i == 1 ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneFavorite;
        IWXAPI api = WXAPIFactory.createWXAPI(this, Constant.ID.WeChat_APP_ID, false);

        api.sendReq(req);
    }

    private void shareXVideo(int i) {//0 聊天1 朋友圈 其他收藏

        String imageFileName = IMAGE_NAME + ".jpg";
        File externalCacheDir = this.getExternalCacheDir();
        String fileName = externalCacheDir.getPath() + "/" + imageFileName;


        WXVideoObject miniProgramObj = new WXVideoObject();
        miniProgramObj.videoUrl = shareBody.getGlobal().getShare().getLink(); // 音乐
/*
        miniProgramObj.miniprogramType = WXMiniProgramObject.MINIPTOGRAM_TYPE_RELEASE;// 正式版:0，测试版:1，体验版:2
*/
        WXMediaMessage msg = new WXMediaMessage(miniProgramObj);
        msg.title = shareBody.getGlobal().getShare().getTitle();                 // 小程序消息title
        msg.description = shareBody.getGlobal().getShare().getDesc();
        Bitmap bitmap = BitMapUtil.getBitmap(fileName);

        msg.thumbData = BitMapUtil.compressBitmap(bitmap, 30 * 1024);// 小程序消息desc

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "video";
        req.message = msg;
        req.scene = i == 0 ? SendMessageToWX.Req.WXSceneSession : i == 1 ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneFavorite;
        IWXAPI api = WXAPIFactory.createWXAPI(this, Constant.ID.WeChat_APP_ID, false);

        api.sendReq(req);
    }

    private void shareXMusic(int i) {//0 聊天1 朋友圈 其他收藏

        String imageFileName = IMAGE_NAME + ".jpg";
        File externalCacheDir = this.getExternalCacheDir();
        String fileName = externalCacheDir.getPath() + "/" + imageFileName;


        WXMusicObject miniProgramObj = new WXMusicObject();
        miniProgramObj.musicUrl = shareBody.getGlobal().getShare().getLink(); // 音乐
/*
        miniProgramObj.miniprogramType = WXMiniProgramObject.MINIPTOGRAM_TYPE_RELEASE;// 正式版:0，测试版:1，体验版:2
*/
        WXMediaMessage msg = new WXMediaMessage(miniProgramObj);
        msg.title = shareBody.getGlobal().getShare().getTitle();                 // 小程序消息title
        msg.description = shareBody.getGlobal().getShare().getDesc();
        Bitmap bitmap = BitMapUtil.getBitmap(fileName);

        msg.thumbData = BitMapUtil.compressBitmap(bitmap, 30 * 1024);// 小程序消息desc

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "music";
        req.message = msg;
        req.scene = i == 0 ? SendMessageToWX.Req.WXSceneSession : i == 1 ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneFavorite;
        IWXAPI api = WXAPIFactory.createWXAPI(this, Constant.ID.WeChat_APP_ID, false);

        api.sendReq(req);
    }

    private void shareXCX() {

        String imageFileName = IMAGE_NAME + ".jpg";
        File externalCacheDir = this.getExternalCacheDir();
        String fileName = externalCacheDir.getPath() + "/" + imageFileName;


        WXMiniProgramObject miniProgramObj = new WXMiniProgramObject();
        miniProgramObj.webpageUrl = shareBody.getGlobal().getShare().getLink(); // 兼容低版本的网页链接
/*
        miniProgramObj.miniprogramType = WXMiniProgramObject.MINIPTOGRAM_TYPE_RELEASE;// 正式版:0，测试版:1，体验版:2
*/
        miniProgramObj.userName = shareBody.getGlobal().getShare().getMiniAppId();     // 小程序原始id
        miniProgramObj.path = shareBody.getGlobal().getShare().getPath();            //小程序页面路径
        WXMediaMessage msg = new WXMediaMessage(miniProgramObj);
        msg.title = shareBody.getGlobal().getShare().getTitle();                 // 小程序消息title
        msg.description = shareBody.getGlobal().getShare().getDesc();
        Bitmap bitmap = BitMapUtil.getBitmap(fileName);

        msg.thumbData = BitMapUtil.compressBitmap(bitmap, 100 * 1024);// 小程序消息desc

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "webpage";
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;  // 目前支持会话
        IWXAPI api = WXAPIFactory.createWXAPI(this, Constant.ID.WeChat_APP_ID, false);

        api.sendReq(req);
    }

    private void share(int i) {
        String imageFileName = IMAGE_NAME + ".jpg";
        File externalCacheDir = this.getExternalCacheDir();
        String fileName = externalCacheDir.getPath() + "/" + imageFileName;
        String sharefileName = externalCacheDir.getPath() + "/12306" + imageFileName;


        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = shareBody.getGlobal().getShare().getLink();
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = shareBody.getGlobal().getShare().getTitle();
        msg.description = shareBody.getGlobal().getShare().getDesc();
        Bitmap bitmap = BitMapUtil.getBitmap(fileName);
        /*Bitmap bitmap1 = BitMapUtil.compressBitmap(bitmap, 31*1024);
        BitMapUtil.saveBitmap2File(bitmap1,sharefileName);
        Bitmap bitmap2 = BitMapUtil.getBitmap(sharefileName);*/
        //Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.mipmap.share);
        msg.thumbData = BitMapUtil.compressBitmap(bitmap, 31 * 1024);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "webpage";
        req.message = msg;
        req.scene = i == 0 ? SendMessageToWX.Req.WXSceneSession : i == 1 ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneFavorite;
        IWXAPI api = WXAPIFactory.createWXAPI(this, Constant.ID.WeChat_APP_ID, false);
        api.sendReq(req);

    }

    public static String IMAGE_NAME = "sharePhoto";

    //根据网络图片url路径保存到本地
    public File saveImageToSdCard(Context context, String image) {
        boolean success = false;
        File file = null;
        try {

            file = createStableImageFile(context);
            Bitmap bitmap = null;
            URL url = new URL(image);
            HttpURLConnection conn = null;
            conn = (HttpURLConnection) url.openConnection();
            InputStream is = null;
            is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);

            FileOutputStream outStream;

            outStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.flush();
            outStream.close();
            success = true;
            //MediaStore.Images.Media.insertImage(context.getContentResolver(), image, file.getName(), "你对图片的描述");

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (success) {

            return file;
        } else {
            return null;
        }
    }

    //创建相册
    public static File createXiangCeImageFile(Context context) throws IOException {
        String imageFileName = IMAGE_NAME + ".jpg";

        String fileName;
        File file;
        if (Build.BRAND.equals("Xiaomi")) { // 小米手机
            fileName = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/" + imageFileName;
        } else {  // Meizu 、Oppo
            fileName = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/" + imageFileName;
        }
        file = new File(fileName);

        if (file.exists()) {
            file.delete();
        }
        return file;
    }

    //创建本地保存路径
    public static File createStableImageFile(Context context) throws IOException {

        String imageFileName = IMAGE_NAME + System.currentTimeMillis() + ".jpg";
        File storageDir = context.getExternalCacheDir();
        File image = new File(storageDir, imageFileName);
        return image;
    }


    //版本更新
    private void upgrade() {
        Map<String, String> map = new HashMap<>();
        map.put("force", "true");

        OkHttpUtil.getJSON(this, Constant.URL.upgrade, map, this);

    }

    /**
     * 更新缓存数量
     */
    private void updateCacheSize() {
        float cache = (CacheUtil.cacheSize() + ConfigConstants.cacheSize()) / 1024.0F;
        String cacheSize;
        if (cache < 1024) {
            if (cache <= 0) {
                cacheSize = "0K";
            } else {
                cacheSize = decimal.format(cache) + "K";
            }
        } else {
            cacheSize = decimal.format(cache / 1024) + "M";
        }
        tvCache.setText(cacheSize);
    }

    @Override
    public void onResponse(String url, String json) {
        if (!TextUtils.isEmpty(json)) {

            if (Constant.URL.upgrade.equals(url)) {
                dismissLoading();
                LogeUtil.e("upgrade", json);
                final UpgradeEntity upgradeEntity = gson.fromJson(json, UpgradeEntity.class);
                if ("200".equals(upgradeEntity.getCode())) {
                    if (!upgradeEntity.getBody().isUpgradeable()) {
                        ToastUtil.initToast(this, "已是最新版本");

                    }
                    isForce = false;
                    versionName = upgradeEntity.getBody().getBundleVersion();
                    String charStr = versionName.charAt(0) + "";
                    String ver = versionName;
                    if ("V".equals(charStr) || "v".equals(charStr)) {
                        ver = versionName.substring(1, versionName.length());
                    }
                    if ("V".equals(charStr)) {
                        isForce = true;
                    }
                    int isNeedUpdate = PhoneUtil.isNeedUpdate(PhoneUtil.getAppVersion(this), ver);
                    switch (isNeedUpdate) {
                        case 2://需要强制更新
                            isForce = true;
                        case 1://需要更新

                            String newContent = upgradeEntity.getBody().getMessage() + "</br>(建议在wifi下更新)";
                            NewVersionDialog newDialog = NewVersionDialog.newInstance(ver,
                                    newContent.replace("</br>", "\n"), isForce == true ? "force" : "取消", "更新");
                            newDialog.setOnItemClickListener(new BaseDialog.OnItemClickListener() {
                                @Override
                                public void onItemClick(View v) {
                                    downLoadApp(upgradeEntity.getBody().getBundleVersion(), upgradeEntity.getBody().getUrl());
                                }
                            });
                            newDialog.show(getFragmentManager());
                            break;
                        default: {
                            ToastUtil.initToast(this, "已是最新版本");
                        }
                        break;
                    }
                }
            }
        }
    }

    @Override
    public void onFailure(String url, String error) {

    }

    private void downLoadApp(String versionName, String url) {
        downloadDialog = DownloadDialog.newInstance(PhoneUtil.getAppName(this) + versionName, isForce);
        downloadDialog.show(getFragmentManager(), "download");
        filePath = Environment.getExternalStorageDirectory() + "/Download/" + PhoneUtil.getAppName(this) +
                "_" + versionName + ".apk";
        Log.e("loge", "Download: " + filePath);
        OkHttpUtil.fileDownload(url, filePath, this, new OkHttpUtil.OnDataListener() {
            @Override
            public void onResponse(String url, String json) {//下载完成
                TextView btnInstall = downloadDialog.getBtnInstall();
                btnInstall.setSelected(true);
                btnInstall.setText("安装");
                btnInstall.setClickable(true);
                btnInstall.setOnClickListener(SettingActivity.this);
                jumpInstall();
            }

            @Override
            public void onFailure(String url, String error) {
            }
        });
    }

    private void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        jumpInstall();
    }

    /**
     * 跳转到安装页面
     */
    private void jumpInstall() {
        File apkFile = new File(filePath);
        if (apkFile.exists()) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");

            } else {

                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);//给目标应用一个临时的授权
                Uri uriForFile = FileProvider.getUriForFile(this, PhoneUtil.getAppProcessName(this) + ".FileProvider", apkFile);
                intent.setDataAndType(uriForFile, "application/vnd.android.package-archive");

            }
            startActivity(intent);
            android.os.Process.killProcess(android.os.Process.myPid());


        }
    }

    @Override
    public void onProgress(final int rate) {
        ProgressBar pb = downloadDialog.getProgressBar();
        final TextView btnInstall = downloadDialog.getBtnInstall();
        if (pb != null) {
            pb.setProgress(rate);
        }
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (btnInstall != null) {
                    btnInstall.setText("下载中(" + rate + "%)");
                    if (rate == 100) {
                        TextView btnInstall = downloadDialog.getBtnInstall();
                        btnInstall.setSelected(true);
                        btnInstall.setText("安装");
                        btnInstall.setClickable(true);
                        btnInstall.setOnClickListener(SettingActivity.this);
                        jumpInstall();
                    }
                }
            }
        });
    }

    private void clearData() {
        PreferencesUtil.clearData(this);
        Intent intent = new Intent(this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    @Override
    public void onItemClick(View v) {
        switch (v.getId()) {

            case R.id.tv_Swx: {

                if ("mini".equals(shareBody.getGlobal().getShare().getType())) {
                    shareXCX();
                } else if ("music".equals(shareBody.getGlobal().getShare().getType())) {
                    shareXMusic(0);

                } else if ("video".equals(shareBody.getGlobal().getShare().getType())) {
                    shareXVideo(0);

                } else if ("link".equals(shareBody.getGlobal().getShare().getType())) {
                    share(0);


                } else if ("text".equals(shareBody.getGlobal().getShare().getType())) {
                    shareXText(0);


                } else if ("image".equals(shareBody.getGlobal().getShare().getType())) {
                    shareXPhoto(0);


                }
                //share(0);
            }
            break;
            case R.id.tv_Spyq: {

                if ("mini".equals(shareBody.getGlobal().getShare().getType())) {
                    share(1);
                } else if ("music".equals(shareBody.getGlobal().getShare().getType())) {
                    shareXMusic(1);

                } else if ("video".equals(shareBody.getGlobal().getShare().getType())) {
                    shareXVideo(1);

                } else if ("link".equals(shareBody.getGlobal().getShare().getType())) {
                    share(1);


                } else if ("text".equals(shareBody.getGlobal().getShare().getType())) {
                    shareXText(1);


                } else if ("image".equals(shareBody.getGlobal().getShare().getType())) {
                    shareXPhoto(1);

                }


            }
            break;
            case R.id.tv_Ssave: {
                if ("mini".equals(shareBody.getGlobal().getShare().getType())) {
                    share(2);

                } else if ("music".equals(shareBody.getGlobal().getShare().getType())) {
                    shareXMusic(2);

                } else if ("video".equals(shareBody.getGlobal().getShare().getType())) {
                    shareXVideo(2);

                } else if ("link".equals(shareBody.getGlobal().getShare().getType())) {
                    share(2);


                } else if ("text".equals(shareBody.getGlobal().getShare().getType())) {
                    shareXText(2);


                } else if ("image".equals(shareBody.getGlobal().getShare().getType())) {
                    shareXPhoto(2);

                }

            }
            break;
        }
    }
}
